package com.logistics.modules.transportationManagement.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

import cn.hutool.core.collection.CollUtil;
import com.logistics.common.utils.ExcelUtiles;
import com.logistics.modules.transportationManagement.excel.ExcelMovePlan;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.logistics.modules.transportationManagement.entity.MoveplanEntity;
import com.logistics.modules.transportationManagement.service.MoveplanService;
import com.logistics.common.utils.PageUtils;
import com.logistics.common.utils.R;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author HURRICANE
 * @email HURRICANE@nb.com
 * @date 2022-05-14 10:48:35
 */
@RestController
@RequestMapping("transportationManagement/moveplan")
public class MoveplanController {

    @Autowired
    private MoveplanService moveplanService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("transportationManagement:moveplan:list")
    public R list(@RequestParam Map<String, Object> params){

        PageUtils page = moveplanService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取结算的数据和计算逾期费用并保存到数据库
     */
    @RequestMapping("/getSettlementAndSaveDatabase")
    public R getSettlementAndSaveDatabase(@RequestParam Map<String, Object> params){
        // 得到数据
        return moveplanService.getSettlementAndSaveDatabase(params);
    }

    /**
     * 结算运输计划
     */
    @PostMapping("/settlementShippingPlan")
    public R settlementShippingPlan(@RequestBody Map<String, Object> params){
        // 实现运输计划结算功能
        moveplanService.settlementShippingPlan(params);

        return R.ok();
    }


    /**
     * 进行入库操作并生成运输订单
     *
     * @param params 参数个数
     * @return {@link R}
     */
    @RequestMapping("/save")
    @RequiresPermissions("transportationManagement:moveplan:save")
    public R save(@RequestBody Map<String, Object> params){

        // 进行入库操作并生成运输订单
        moveplanService.warehousingAndGeneratingShippingPlans(params);

        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("transportationManagement:moveplan:info")
    public R info(@PathVariable("id") Integer id){

		MoveplanEntity moveplan = moveplanService.getById(id);

        return R.ok().put("moveplan", moveplan);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("transportationManagement:moveplan:update")
    public R update(@RequestBody MoveplanEntity moveplan){

		moveplanService.updateById(moveplan);

        return R.ok();
    }



    /**
     * 出口xls
     *
     * @param params   参数个数
     * @param response 响应
     * @return {@link ModelAndView}
     */
    @PostMapping("/exportXls")
    @RequiresPermissions("transportationManagement:moveplan:list")
    public void exportXls(@RequestBody Map<String, Object> params, HttpServletResponse response) {
        // 读取数据
        ExcelMovePlan move = moveplanService.downloadExcel(params);
            ArrayList<ExcelMovePlan> moveplanEntities = CollUtil.newArrayList(move);

        ExcelUtiles.exportExcel(moveplanEntities, "运输计划", "运输计划", ExcelMovePlan.class, "测试.xls", response);
    }

    /**
     * 拿到根据时间段不同的数据
     */
    @RequestMapping("/getDataByTime")
    @RequiresPermissions("transportationManagement:moveplan:list")
    public R getDataByTime(@RequestParam Map<String, Object> params){
        return moveplanService.getDataByTime(params);
    }

    /**
     * 拿到运输计划饼状图数据
     */
    @RequestMapping("/getPieData")
    @RequiresPermissions("transportationManagement:moveplan:list")
    public R getPieData(){
        // 拿到运输计划饼状图数据
        return moveplanService.getPieData();
    }

}
